package com.hopu.jq_day04.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import org.apache.ibatis.session.SqlSession;

import com.hopu.jq_day04.model.StuInfo;
import com.hopu.jq_day04.model.Users;
import com.hopu.jq_day04.util.GetSqlSession;

/**
 * Servlet implementation class GetUsersServlet
 */
@WebServlet("/GetUsersServlet")
public class GetUsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetUsersServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        System.out.println("type is : " + type);

        if("login".equals(type)){
            login(response);
        }else if("find".equals(type)){
            find(response);
        }else if("update".equals(type)){
            update(request, response);
        }else if("delete".equals(type)){
            delete(request);
        }else if("insert".equals(type)){
            insert(request,response);
        }else if("queryIndex".equals(type)){
            queryIndex(request, response);
        }else if("queryCount".equals(type)){
            Gson gson = new Gson();
            SqlSession sqlSession  = GetSqlSession.getSqlSession();
            int count = sqlSession.selectOne("users.sql.queryCount");
            GetSqlSession.commit();
            String returnInfo = gson.toJson(count);
            response.getWriter().write(returnInfo);
        }
	}

    private void queryIndex(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Gson gson = new Gson();
        int start = Integer.valueOf(request.getParameter("start"));
        int count = Integer.valueOf(request.getParameter("count"));
        start = (start-1)*count;
        Map<String,Object> getValue = new HashMap();
        getValue.put("start",start);
        getValue.put("count",count);
        System.out.println("start:" + start + " count:" + count);
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        List<StuInfo> pageQuery = sqlSession.selectList("users.sql.queryIndex",getValue);
        GetSqlSession.commit();
        //get json
        String returnJson = gson.toJson(pageQuery);
        System.out.println(returnJson);
        response.getWriter().write(returnJson);
    }

    private void insert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //=================================================
        int count = 0;
        StuInfo stuInfo = null;
        int sid = Integer.valueOf(request.getParameter("sid"));
        String sname = request.getParameter("sname");
        String sex = request.getParameter("sex");
        //========================================================
        int age = Integer.valueOf(request.getParameter("age"));
        String classes = request.getParameter("classes");
        String sgroups = request.getParameter("sgroups");
        String dorm = request.getParameter("dorm");
        String tel = request.getParameter("tel");
        stuInfo = new StuInfo(sid,sname,sex,age,classes,sgroups,dorm,tel);
        System.out.println("insret = " + stuInfo.toString());
        //-------------------------------------------------------
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        count = sqlSession.insert("users.sql.insertStu",stuInfo);
        System.out.println("update count values = "+ count);
        GetSqlSession.commit();
//        request.getRequestDispatcher("page/info.jsp").forward(request,response);
    }

    private void delete(HttpServletRequest request) {
        int sid = Integer.valueOf(request.getParameter("sid"));
        System.out.println("delete id :" + sid);
        //-------------------------------------------------------
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        int count = sqlSession.delete("users.sql.deleteStu",sid);
        GetSqlSession.commit();
        System.out.println("delete count values: " + count);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int count = 0;
        StuInfo stuInfo = null;
        int sid = Integer.valueOf(request.getParameter("sid"));
        String sex = request.getParameter("sex");
        String sname = request.getParameter("sname");
        int age = Integer.valueOf(request.getParameter("age"));
        String classes = request.getParameter("classes");
        String sgroups = request.getParameter("sgroups");
        String tel = request.getParameter("tel");
        String dorm = request.getParameter("dorm");
        stuInfo = new StuInfo(sid,sname,sex,age,classes,sgroups,dorm,tel);
        System.out.println(stuInfo.toString());
        //-------------------------------------------------------
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        count = sqlSession.update("users.sql.insertStu",stuInfo);
        System.out.println("insert count values = "+ count);
        GetSqlSession.commit();
    }

    private void find(HttpServletResponse response) throws IOException {
        System.out.println("enter the find");
        Gson gson = new Gson();
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        List<StuInfo> list = sqlSession.selectList("users.sql.querlAllInfo");
        GetSqlSession.commit();
        if(list !=null){
            for (StuInfo stuInfo:
                 list) {
                System.out.println(stuInfo.toString());
            }
            String returnString = gson.toJson(list);
            response.getWriter().write(returnString);
        }
    }

    private void login(HttpServletResponse response) throws IOException {
        Users users ;
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        users = sqlSession.selectOne("users.sql.login", "1");
        GetSqlSession.commit();
        System.out.println(users.toString());
        if(users != null){
            Cookie cookie = new Cookie("user",users.toString());
            response.sendRedirect("page/main.jsp");
        }else{
            response.sendRedirect("index.jsp");
        }
    }

    /**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
